import time
from unittest import TestCase

import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestSignUp(TestCase):
    def test_signup(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "delete from user where username='wangwu';"
        print(sql)
        cursor.execute(sql)
        connect.commit()
        time.sleep(1)
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertNotIn(('wangwu',), x)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get('http://localhost:90')
        chromeDriver.find_element(By.XPATH, '//html/body/div/div/div/div[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="username"]').send_keys('wangwu')
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="password"]').send_keys('123456')
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="repassword"]').send_keys('123456')
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="login_btn"]').click()
        time.sleep(1)
        url = chromeDriver.current_url
        if url != 'http://localhost:90/':
            chromeDriver.save_screenshot('signup1.jpg')
        self.assertEqual(url, 'http://localhost:90/')

    def test_fail_signup(self):
        chromeDriver = webdriver.Chrome()
        chromeDriver.get('http://localhost:90')

        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'wangwu' in x[i - 1]:
                break
            elif x[len(x) - 1]:
                chromeDriver.find_element(By.XPATH, '//html/body/div/div/div/div[2]/a').click()
                time.sleep(1)
                chromeDriver.find_element(By.XPATH, '//*[@id="username"]').send_keys('wangwu1')
                time.sleep(1)
                chromeDriver.find_element(By.XPATH, '//*[@id="password"]').send_keys('123456')
                time.sleep(1)
                chromeDriver.find_element(By.XPATH, '//*[@id="repassword"]').send_keys('123456')
                time.sleep(1)
                chromeDriver.find_element(By.XPATH, '//*[@id="login_btn"]').click()
                break
        connect.commit()
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertIn(('wangwu1',), x)

        chromeDriver.find_element(By.XPATH, '//html/body/div/div/div/div[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="username"]').send_keys('wangwu1')
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="password"]').send_keys('123456')
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="repassword"]').send_keys('123456')
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="login_btn"]').click()
        time.sleep(1)
        url = chromeDriver.current_url
        if url == 'http://localhost:90/':
            chromeDriver.save_screenshot('signup2.jpg')
        print(url)
        self.assertNotEqual(url, 'http://localhost:90/')

    def test_repassword(self):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "delete from user where username='wangwu';"
        print(sql)
        cursor.execute(sql)
        connect.commit()
        time.sleep(1)
        sql = "select username from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        self.assertNotIn(('wangwu',), x)

        chromeDriver = webdriver.Chrome()
        chromeDriver.get('http://localhost:90')
        chromeDriver.find_element(By.XPATH, '//html/body/div/div/div/div[2]/a').click()
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="username"]').send_keys('wangwu')
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="password"]').send_keys('123456')
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="repassword"]').send_keys('1234567')
        time.sleep(1)
        chromeDriver.find_element(By.XPATH, '//*[@id="login_btn"]').click()
        rep = chromeDriver.find_element(By.XPATH, '//html/body/div/div/div/form/div[3]/span[1]').text
        url = chromeDriver.current_url
        if url == 'http://localhost:90/':
            chromeDriver.save_screenshot('signup3.jpg')
        self.assertEqual(rep, '两次密码不相同')
        self.assertNotEqual(url, 'http://localhost:90/')
